package com.twu.biblioteca;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: ysotirov
 * Date: 29/01/13
 * Time: 17:04
 * To change this template use File | Settings | File Templates.
 */
public class ReserveBookForCollection extends Option {
    int bookId = -1;

    public ReserveBookForCollection(IOInterface ioHndl, Biblioteca bibl) {
        super(ioHndl,bibl);
    }

    public void setBookId(int id)
    {
        bookId = id;
    }

    private int getBookId()
    {
        return bookId;
    }



    @Override
    public void executeOption() {
        //Option reserveBook = new ReserveBook();

        if (!biblioteca.activeMember.isLoggedIn())
            requestLogIn();
        ioHandler.queueMsg("Please enter the id of the book");
        ioHandler.Print();
        reserveBookById(ioHandler.getInput());


    }

    public void requestLogIn() {
        ioHandler.queueMsg("You need to log in to reserve a book. Please enter your credentials:");
        ioHandler.queueMsg("Username");
        ioHandler.queueMsg(biblioteca.activeMemberID);
        while (!biblioteca.activeMember.isLoggedIn())
        {
        ioHandler.queueMsg("Please enter your password");
        ioHandler.Print();
        biblioteca.activeMember.logIn(biblioteca.activeMemberID, ioHandler.getUserCredentials());
        }
        ioHandler.queueMsg("Successful authentication!");
        ioHandler.Print();
    }

    public ArrayList<String> getArray(){
        ArrayList<String> list = new ArrayList<String>();

        for (int i = 0; i<this.biblioteca.booksList.size(); i++)
            list.add(this.biblioteca.booksList.get(i).Title);
        return list;

    }

    @Override
    public int getUserChoice(){
        int opt = ioHandler.getInput();
        if (!validInput(opt, getArray()))
        {
            ioHandler.queueMsg("Please input a valid option");
            ioHandler.Print();
            opt = getUserChoice();
        }
        return opt;
    }

    private void reserveBookById(int id) {
        String msg = this.biblioteca.booksList.get(id-1).reserveBook();
        ioHandler.queueMsg(msg);
        if (msg == "Thank You! Enjoy the book.")
        {
            biblioteca.activeMember.reservedBooks.add(this.biblioteca.booksList.get(id-1).Title);
            biblioteca.booksList.get(id-1).userID=biblioteca.activeMemberID;
        }
        ioHandler.Print();
    }

}
